#include "window_common.h"
#include "gkt_network.h"

#if defined(GKT_CONFIG_NETWORK_ENABLE) && GKT_CONFIG_NETWORK_ENABLE

enum {
	TIMER_ID_TIMEOUT = 0,
	TIMER_ID_DELAY_RETURN,
	TIMER_ID_UPGRADE_CANCEL_RESET,
	TIMER_ID_MAX
};

enum {
	WIDGET_ID_TITLE = 0,
	WIDGET_ID_HINT,
	WIDGET_ID_KEEP_POWER,
	WIDGET_ID_RETURN,
	WIDGET_ID_MAX
};

#define WINDOW_NETWORK_FIREWARE_UPGREDE_DELAY_RETURN_TIME_MS 2000

GKT_WINDOW_WIDGET_DESCS_DEFINE(firmware_upgrade) = {
	[WIDGET_ID_TITLE] = GKT_WIDGET_LABEL_DESC(
		GKT_WIDGET_F_ALIGN_MIDDLE | GKT_WIDGET_F_ALIGN_CENTER | GKT_WIDGET_F_TITLE_MARK,
		WINDOW_FONT_NORMAL_SIZE,
		WINDOW_TITLE_FG_COLOR, WINDOW_TITLE_BG_COLOR,
		0, 0,
		GKT_SCREEN_WIDTH, WINDOW_WIDGET_NORMAL_HEIGHT,
		WINDOW_TEXT_ID_upgrade_firmware),
	[WIDGET_ID_HINT] = GKT_WIDGET_LABEL_DESC(
		GKT_WIDGET_F_ALIGN_MIDDLE,
		WINDOW_FONT_NORMAL_SIZE,
		WINDOW_DEFAULT_FG_COLOR, WINDOW_DEFAULT_BG_COLOR,
		0, WINDOW_WIDGET_NORMAL_HEIGHT,
		GKT_SCREEN_WIDTH, WINDOW_WIDGET_NORMAL_HEIGHT,
		WINDOW_TEXT_ID_being_upgrade_firmware),
	[WIDGET_ID_KEEP_POWER] = GKT_WIDGET_LABEL_DESC(
		GKT_WIDGET_F_ALIGN_MIDDLE,
		WINDOW_FONT_NORMAL_SIZE,
		WINDOW_HINT_IMPORTANT_FG_COLOR, WINDOW_DEFAULT_BG_COLOR,
		0, WINDOW_WIDGET_NORMAL_HEIGHT * 2,
		WINDOW_WIDGET_DEFAULT_WIDTH, WINDOW_WIDGET_NORMAL_HEIGHT,
		WINDOW_TEXT_ID_please_keep_the_power_on
	),
	[WIDGET_ID_RETURN] = GKT_WIDGET_LABEL_DESC(
		GKT_WIDGET_F_ALIGN_MIDDLE,
		WINDOW_FONT_NORMAL_SIZE,
		WINDOW_DEFAULT_FG_COLOR, WINDOW_DEFAULT_BG_COLOR,
		0, WINDOW_WIDGET_NORMAL_HEIGHT * 3,
		WINDOW_WIDGET_DEFAULT_WIDTH, WINDOW_WIDGET_NORMAL_HEIGHT,
		WINDOW_TEXT_ID_return
	)
};

typedef struct _window_network_fireware_upgrade{
	uint32_t cancel_flag;
}window_network_fireware_upgrade_s;
static window_network_fireware_upgrade_s s_window_network_fireware_upgrade;

static void window_firmware_upgrade_complete(window_result_e result)
{
	uint32_t hint_type;

	if (WINDOW_RESULT_OPERATE_SUCCESS == result)
		hint_type = WINDOW_DIALOG_HINT_SUCCESS;
	else if (WINDOW_RESULT_OPERATE_TIMEOUT == result)
		hint_type = WINDOW_DIALOG_HINT_TIMEOUT;
	else
		hint_type = WINDOW_DIALOG_HINT_FAILED;

	gkt_window_timeout_enable("window_firmware_upgrade - complete");
	gkt_window_system_standby_enable();

	gkt_window_timer_clear(TIMER_ID_TIMEOUT);

	window_dialog_show_big_prompt(hint_type, 
			WINDOW_TEXT_ID_being_upgrade_firmware, 
			gc_window_result_text_ids[result],
			gc_window_result_voice_ids[result]);
	gkt_window_exit();
}

static int window_firmware_upgrade_event_handler(
				uint32_t major_type, uint32_t minor_type,
				uint32_t id, uint32_t data)
{
	int retval = GKT_EVENT_HANDLE_COMPLETE;

	switch(major_type) {
		case GKT_EVENT_WINDOW:
			if (GKT_WINDOW_ACTIVE == minor_type) {
				s_window_network_fireware_upgrade.cancel_flag = 0;
				gkt_window_system_standby_disable();
				gkt_window_timeout_disable("window_firmware_upgrade - active");
				window_common_mask_all();
				if (gkt_network_is_connected()) {
					retval = gkt_network_firmware_upgrade();
					if ((GKT_SUCCESS == retval) || (GKT_EINPROCESS == retval)) {
						gkt_window_timer_set(TIMER_ID_DELAY_RETURN, WINDOW_NETWORK_FIREWARE_UPGREDE_DELAY_RETURN_TIME_MS, 0, 0);
						gkt_widget_set_showable_by_id(WIDGET_ID_KEEP_POWER, 1);
						gkt_widget_show_by_id(WIDGET_ID_KEEP_POWER);
						gkt_window_timer_set(TIMER_ID_TIMEOUT, GKT_NETWORK_FIRMWARE_UPGRADE_TIMEOUT_SEC*1000, 0, 0);
					}
					else
						window_firmware_upgrade_complete(WINDOW_RESULT_OPERATE_FAILED);
				}
				else {
					gkt_widget_set_data_by_id(WIDGET_ID_HINT, WINDOW_TEXT_ID_please_connect_network);
					window_firmware_upgrade_complete(WINDOW_RESULT_OPERATE_FAILED);
				}
			}
			break;
		case GKT_EVENT_SYSTEM:
			if (GKT_WINDOW_SYSTEM_NETWORK == minor_type) {
				if (GKT_NETWORK_NOTIFY_TYPE_RUNNING_STATUS == id) {
					gkt_info("window_firmware_upgrade - (data:%d)\n", data);
					if (GKT_NETWORK_RUNNING_ST_UPGRADED == data)
						window_firmware_upgrade_complete(WINDOW_RESULT_OPERATE_SUCCESS);
					else if (GKT_NETWORK_RUNNING_ST_UPGRADE_FAILED == data)
						window_firmware_upgrade_complete(WINDOW_RESULT_OPERATE_FAILED);
					else if (GKT_NETWORK_RUNNING_ST_UPGRADE_CANCEL_FAILED == data)
						gkt_window_timer_set(TIMER_ID_UPGRADE_CANCEL_RESET, 1000, 0, 0);
					else if ((GKT_NETWORK_RUNNING_ST_UPGRADE_CANCEL_FORBID == data)
						|| (GKT_NETWORK_RUNNING_ST_UPGRADING == data))
					{
						window_common_mask_all();
					}
					else if (GKT_NETWORK_RUNNING_ST_UPGRADE_CANCELED == data) {
						gkt_window_timer_clear(TIMER_ID_UPGRADE_CANCEL_RESET);
						gkt_window_timer_clear(TIMER_ID_TIMEOUT);
						gkt_window_timeout_enable("window_firmware_upgrade - canceled");
						gkt_window_system_standby_enable();
						gkt_window_exit();
					}
				}
			}
		case GKT_EVENT_KEY:
			if (GKT_KEY_ASTERISK == minor_type) {
				if (!s_window_network_fireware_upgrade.cancel_flag) {
					retval = gkt_network_firmware_upgrade_cancel();
					if (GKT_SUCCESS == retval)
						s_window_network_fireware_upgrade.cancel_flag = 1;
					else {
						gkt_error("window_firmware_upgrade - cancel error. retval:%d\n", retval);
						s_window_network_fireware_upgrade.cancel_flag = 0;
					}
				}
				retval = GKT_EVENT_HANDLE_COMPLETE;
			}
			break;
		case GKT_EVENT_TIMER:
			if (TIMER_ID_TIMEOUT == minor_type)
				window_firmware_upgrade_complete(WINDOW_RESULT_OPERATE_TIMEOUT);
			else if (TIMER_ID_UPGRADE_CANCEL_RESET)
				s_window_network_fireware_upgrade.cancel_flag = 0;
			else if (TIMER_ID_DELAY_RETURN == minor_type)
				window_common_mask_except_return();
		default:
			break;
	}

	return retval;
}
GKT_WINDOW_DESC_DEFINE(firmware_upgrade);

#endif




